package qualomelhor.server;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.*; 
import qualomelhor.database.LinkBlobImage;

public class ExibeImagemServlet extends HttpServlet { 

	private static final long serialVersionUID = 0;

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 

		Long idLinkBlob = Long.parseLong((String)req.getAttribute("idLinkBlob"));
		DAO d = new DAO();

		LinkBlobImage l = d.getLinkBlobImage(idLinkBlob);

		if (l != null) {
			resp.setContentType("image/png");
			OutputStream outStream = resp.getOutputStream();  
		  	outStream.write(l.getImage().getBytes());  
		  	outStream.flush();
		  	outStream.close();
		} else {
			resp.sendRedirect("/images/boneco.png");
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doPost(req, resp);
	}
}